package com.ruoyi.web.controller.system;

import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.AdminUpdownBranch;
import com.ruoyi.system.service.IAdminUpdownBranchService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 上下分管理Controller
 * 
 * @author ruoyi
 * @date 2020-11-10
 */
@RestController
@RequestMapping("/system/updownBranch")
public class AgentUpdownBranchController extends BaseController
{
    @Autowired
    private IAdminUpdownBranchService adminUpdownBranchService;

    /**
     * 查询上下分管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:updownBranch:list')")
    @GetMapping("/list")
    public TableDataInfo list(AdminUpdownBranch adminUpdownBranch)
    {
        startPage();
        List<AdminUpdownBranch> list = adminUpdownBranchService.selectAdminUpdownBranchListByAdmin(adminUpdownBranch);
        return getDataTable(list);
    }

    /**
     * 导出上下分管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:updownBranch:export')")
    @Log(title = "上下分管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(AdminUpdownBranch adminUpdownBranch)
    {
        List<AdminUpdownBranch> list = adminUpdownBranchService.selectAdminUpdownBranchList(adminUpdownBranch);
        ExcelUtil<AdminUpdownBranch> util = new ExcelUtil<AdminUpdownBranch>(AdminUpdownBranch.class);
        return util.exportExcel(list, "updownBranch");
    }

    /**
     * 获取上下分管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:updownBranch:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(adminUpdownBranchService.selectAdminUpdownBranchById(id));
    }

    /**
     * 新增上下分管理
     */
    @PreAuthorize("@ss.hasPermi('system:updownBranch:add')")
    @Log(title = "上下分管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AdminUpdownBranch adminUpdownBranch)
    {
        return toAjax(adminUpdownBranchService.insertAdminUpdownBranch(adminUpdownBranch));
    }

    /**
     * 修改上下分管理
     */
    @PreAuthorize("@ss.hasPermi('system:updownBranch:edit')")
    @Log(title = "上下分管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AdminUpdownBranch adminUpdownBranch)
    {
        return toAjax(adminUpdownBranchService.updateAdminUpdownBranch(adminUpdownBranch));
    }

    /**
     * 删除上下分管理
     */
    @PreAuthorize("@ss.hasPermi('system:updownBranch:remove')")
    @Log(title = "上下分管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(adminUpdownBranchService.deleteAdminUpdownBranchByIds(ids));
    }

    @GetMapping("/audit")
    public AjaxResult audit(AdminUpdownBranch adminUpdownBranch)
    {
        AjaxResult result=AjaxResult.success();
        int audit = adminUpdownBranchService.audit(adminUpdownBranch);
        if (audit!=1) {
            result=AjaxResult.error("下分失败，余额不足");
        }else{
            if (adminUpdownBranch.getAuditStatus()==1) {
                result=AjaxResult.success("审核成功");
            }else{
                result=AjaxResult.success("驳回成功");
            }
        }
        return result;
    }

}
